package com.wing.res.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.exception.BusinessException;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.res.model.entity.ReceiptRecord;
import com.wing.res.service.ReceiptApplyService;
import com.wing.res.service.ReceiptRecordService;
import com.wing.web.db.config.util.DynamicDataSource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


@Validated
@Api(value = "出库任务管理", tags = "#/storage/receiptApply1")
@RestController("ReceiptApplyController")
@RequestMapping("storage/admin/receiptApply1")
@RequiredArgsConstructor
public class ReceiptApplyController {

    private final ReceiptApplyService receiptRecordService;

    @ApiOperation(value = "查询领料记录列表", notes = "搜索")
    @GetMapping("/list")
    public PageResult<ReceiptRecord> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(name = "workOrderCode", required = false) String workOrderCode,
            @RequestParam(name = "planId", required = false) Long planId,
            @RequestParam(name = "processId", required = false) Long processId,
            @RequestParam(name = "inventoryTypeCode", required = false) String inventoryTypeCode,
            @RequestParam(name = "workOrderCode2", required = false) String workOrderCode2,
            @RequestParam(name = "status", required = false) String status,
            @RequestParam(name = "inventoryTypeId", required = false) String inventoryTypeId,
         /*   @RequestParam(name = "operateType", required = false) String operateType*/
            @RequestParam(name = "receiptWarehouse", required = false) Long receiptWarehouse,
            @RequestParam(name = "creator", required = false) Long creator,
            @RequestParam(name = "startCreateTime", required = false) String startCreateTime,
            @RequestParam(name = "endCreateTime", required = false) String endCreateTime
    ) {
        Page<ReceiptRecord> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("workOrderCode", workOrderCode);
        params.put("planId", planId);
        params.put("processId", processId);
        params.put("inventoryTypeCode", inventoryTypeCode);
        params.put("workOrderCode2", workOrderCode2);
        params.put("status", status);
        params.put("inventoryTypeId", inventoryTypeId);
    /*    params.put("operateType", operateType);*/
        params.put("receiptWarehouse", receiptWarehouse);
        params.put("creator", creator);
        params.put("startCreateTime", startCreateTime);
        params.put("endCreateTime", endCreateTime);

        String databaseUser = DynamicDataSource.USER_PREFIX;
        if (StringUtils.isNotBlank(DynamicDataSource.name.get())) {
            databaseUser = databaseUser + "-" + DynamicDataSource.name.get();
        }
        params.put("databaseUser", databaseUser);
        IPage<ReceiptRecord> list = receiptRecordService.list(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @ApiOperation(value = "添加领料记录", notes = "添加")
    @PostMapping("/add")
    public JsonResult add(@RequestBody ReceiptRecord receiptRecord) {
        try {
            receiptRecord.setOperateType("0");
            receiptRecordService.add(receiptRecord);
        } catch (BusinessException e) {
            return JsonResult.fail(e.getMessage());
        }
        return JsonResult.success("添加成功");
    }

    @ApiOperation(value = "更新领料记录", notes = "更新")
    @PostMapping("/update")
    public JsonResult upadte(ReceiptRecord receiptRecord) {
        receiptRecordService.update(receiptRecord);
        return JsonResult.success("更新成功");
    }

    @ApiOperation(value = "领料记录详情", notes = "详情")
    @GetMapping("/get")
    public JsonResult get(@RequestParam(name = "id") Long id) {
        Map<String, Object> params = new HashMap<String, Object>();
        String databaseUser = DynamicDataSource.USER_PREFIX;
        if (StringUtils.isNotBlank(DynamicDataSource.name.get())) {
            databaseUser = databaseUser + "-" + DynamicDataSource.name.get();
        }
        params.put("databaseUser", databaseUser);
        params.put("id", id);
        return JsonResult.success(receiptRecordService.get(params));
    }

}
